#include <sys/types.h>
#include <unistd.h>
#include "soft_types.h"
#include "config.h"

#if defined(CONFIG_COMMON_LINUX_MSG_QUEUE)
#include "linux_msg/linux_msgQueue.h"
#endif

INT32 main(INT32 argc, CHAR **args)
{
    UCHAR aucRecv[512] = {0};
    ULONG ulRet = STAT_SUCCEED;
    INT32 iProj_id = 0;
    LINUX_MSG_s stQueue;
    LINUX_MSG_ITEM_s *pstInfo = (LINUX_MSG_ITEM_s *)aucRecv;

    UNUSED_PARAM(argc);
    UNUSED_PARAM(args);

    /* Projid [0 ~ 255] */
    iProj_id = getpid() % 256;
    printf("[%s] pid %d\n", __FUNCTION__, iProj_id);

    LINUX_MSG_init(&stQueue);

    ulRet = stQueue.create(iProj_id, &stQueue);
    if (ulRet != STAT_SUCCEED)
    {
        LOG_E("LINUX_MSG create fail, status %lu\n", ulRet);
        return -1;
    }
    
    pstInfo->lType = 1234;
    pstInfo->stData.ulLen = 6;
    memcpy((char *)pstInfo->stData.aucMsgData, "HELLO", 6);
    ulRet = stQueue.send(stQueue.iMsgQueueId, pstInfo);
    if (ulRet != STAT_SUCCEED)
    {
        LOG_E("send fail, status %lu\n", ulRet);
        return -1;
    }

    memset(aucRecv, 0, sizeof(aucRecv));
    pstInfo->lType = 1234;
    pstInfo->stData.ulLen = sizeof(aucRecv);
    ulRet = stQueue.recv(stQueue.iMsgQueueId, pstInfo);
    if (ulRet != STAT_SUCCEED)
    {
        LOG_E("recv fail, status %lu\n", ulRet);
        return -1;
    }

    LOG_I("Recv = [%s]\n", pstInfo->stData.aucMsgData);

    ulRet = stQueue.RemoveID(&stQueue);
    if (ulRet != STAT_SUCCEED)
    {
        LOG_E("RemoveID fail, status %lu\n", ulRet);
        return -1;
    }

    return 0;
}

